#!/usr/bin/env python
# -*- coding: utf-8 -*-

"Link Class mapping to SQL table"

from django.db import models
from django.utils.html import format_html
from labman2.data.subdata.NumValue.models import NumValue
from labman2.data.subdata.ReST.models import ReST


#==============================================================================
# A numerical value, e.g. a universal constant or a simple measurement
class ValueWithUncertainty(NumValue):
    "A numeric data entry with uncertainty"


    uncertainty = models.FloatField(null=True, blank=True,
                                    help_text="Use style like 1.234e-5")

    def __unicode__(self):
        try:
            return format_html(u'{0} ± {1} :{2}',
                               str(self.num_value),
                               str(self.uncertainty),
                               ReST.__unicode__(self))
        except StandardError as err:
            print "ValueWithUncertainty.__unicode__", err
            return u'%s ± %s :%s' % (str(self.num_value),
                                str(self.uncertainty),
                                u"ReST.__unicode__(self)")

    def show_details(self):
        'Return a list of the fields for detailed view'

        info_dict = [("NumValue", self.num_value),
                     ('Uncertainty', self.uncertainty)]
        info_dict.extend(ReST.show_details(self))
        return info_dict
